﻿using System;
using System.Collections.Generic;
using System.Linq;
using TradeSea;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace TradeSea
{
    class Fishing
    {
        System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch();
        ItemList fish = new ItemList();
        Random ran = new Random();
        public bool isFishing = false;
        public bool finishedFishing = false;
        string net;
        Inventory inv;
        List<string> caught = new List<string>();



        public Fishing(Inventory _inv)
        {
            inv = _inv;
            foreach (string s in inv.PrintInvintoryWOamount())
            {
                if (s == "super net")
                { net = s; break; }
                if (s == "good net")
                { net = s; break; }
                if (s == "basic net")
                { net = s; break; }
            }
        }

        public void updateFishing(KeyboardState key)
        {
            if(key.IsKeyDown(Keys.F))
                GoFishing();
            if (timer.ElapsedMilliseconds >= 10000)
                GoFishing();
        }

        public void GoFishing()
        {
            finishedFishing = false;
            isFishing = true;
            jPrint = false;

            if(!timer.IsRunning)
            {
                timer.Reset();
                timer.Start();
            }
            if(timer.ElapsedMilliseconds >= 10000)
            {
            caught = new List<string>();
                int i = 0;
                if(net == "basic net")
                    i = 3;
                else if(net == "good Net")
                    i = 6;
                else if(net == "super Net")
                    i = 9;
                
                while(i != 0)
                {
                    double number = 100* ran.NextDouble();

                       if (number >= 99)
                        caught.Add(fish.KingFish());
                    else if (number >= 95)
                        caught.Add(fish.JohnDory());
                    else if (number >= 90)
                        caught.Add(fish.LemonShark());
                    else if (number >= 85)
                        caught.Add(fish.Cod());
                    else if (number >= 80)
                        caught.Add(fish.Gernid());
                    else if (number >= 0)
                        caught.Add(fish.Snapper());

                    i--;
                }
                if (caught.Count != 0)
                    i = 9 - caught.Count;
                else i = 9;
                while (i != 0)
                {
                    caught.Add("-");
                    i--;
                }
                isFishing = false;
                finishedFishing = true;
            }

        }

        public List<string> getCaught()
        {
            return caught;
        }

        public int secondsLeft()
        {
            if (isFishing)
            {
                if (timer.ElapsedMilliseconds <= 1000)
                    return 10;
                else if (timer.ElapsedMilliseconds <= 2000)
                    return 9;
                else if (timer.ElapsedMilliseconds <= 3000)
                    return 8;
                else if (timer.ElapsedMilliseconds <= 4000)
                    return 7;
                else if (timer.ElapsedMilliseconds <= 5000)
                    return 6;
                else if (timer.ElapsedMilliseconds <= 6000)
                    return 5;
                else if (timer.ElapsedMilliseconds <= 7000)
                    return 4;
                else if (timer.ElapsedMilliseconds <= 8000)
                    return 3;
                else if (timer.ElapsedMilliseconds <= 9000)
                    return 2;
                else if (timer.ElapsedMilliseconds <= 10000)
                    return 1;
                else return 0;

            }
            else
                return 0;
        }

        public bool jPrint = false;

        public void drawFishing(SpriteBatch sb, TSContentManager manager)
        {
            if (!isFishing && !finishedFishing)
            {
                sb.DrawString(manager.ConsoleFont, "Press F to start fishing", new Vector2(10, 10), Color.Red);
            }

            else if (isFishing)
            {
                sb.DrawString(manager.ConsoleFont, secondsLeft().ToString() + " Seconds until we", new Vector2(10, 10), Color.Red);
                sb.DrawString(manager.ConsoleFont, "  bring up the net", new Vector2(10, 25), Color.Red);
            }
                
            else if (finishedFishing)
            {
                Vector2 vec = new Vector2(10, 10);
                sb.DrawString(manager.ConsoleFont, "Press F to start fishing", vec, Color.Red);
                vec = new Vector2(vec.X, vec.Y + 15);
                sb.DrawString(manager.ConsoleFont, "We have finished Fishing", vec, Color.Red);
                vec = new Vector2(vec.X, vec.Y + 15);
                sb.DrawString(manager.ConsoleFont, "Heres what we got:", vec, Color.Red);
                vec = new Vector2(vec.X + 10, vec.Y + 15);

                if (caught.Count != 0)
                {
                    foreach (string s in caught)
                    {
                        if (s != "-" && !jPrint)
                        { inv.addToInventory(s); jPrint = true; }
                        sb.DrawString(manager.ConsoleFont, s, vec, Color.Red);
                        vec = new Vector2(vec.X, vec.Y + 15);
                    }
                }

                else
                {
                    sb.DrawString(manager.ConsoleFont, "You Caught No Fish", vec, Color.Red);
                }
                timer.Stop();
                timer.Reset();
            }
        }
    }
}
